#include <iostream>
#include <fstream>
#include <vector>

#include "../utils/graph.hh"

template <class T>
void bin_read(std::istream& i, T* x)
{
    i.read(reinterpret_cast<char*>(x), sizeof(*x));
    if (!i) throw std::runtime_error{"binary read failed"};
}

template <class T>
void bin_read_n(std::istream& i, T* x, int size)
{
    i.read(reinterpret_cast<char*>(x), sizeof(*x) * size);
    if (!i) throw std::runtime_error{"binary read failed"};
}

template <class T>
void bin_write(std::ostream& o, T* x)
{
    o.write(reinterpret_cast<const char*>(x), sizeof(*x) * sizeof(T));
    if (!o) throw std::runtime_error{"binary write failed"};
}

template <class T>
void bin_write_n(std::ostream& o, T* x, int size)
{
    o.write(reinterpret_cast<char*>(x), sizeof(*x) * size);
    if (!o) throw std::runtime_error{"binary write failed"};
}


int main()
{
    // std::ofstream fout{"tmp", std::ios::binary};
    // std::vector<int> a(9, 44446);
    // bin_write_n(fout, a.data(), a.size());
    // fout.close();

    // std::ifstream fin{"tmp", std::ios::binary};
    // std::vector<int> b(9);
    // bin_read_n(fin, b.data(), b.size());
    // std::cerr << b[8] << "\n";
    // return 0;

    std::string const input[] = {
        "dataset/usa/USA-road-d.E.gr",
        "dataset/usa/USA-road-d.W.gr",
        "dataset/usa/USA-road-d.CTR.gr",
        "dataset/usa/USA-road-d.USA.gr",
    };
    std::string const output[] = {
        "dataset/usa/USA-road-d.E.obj.gr",
        "dataset/usa/USA-road-d.W.obj.gr",
        "dataset/usa/USA-road-d.CTR.obj.gr",
        "dataset/usa/USA-road-d.USA.obj.gr",
    };

    for (auto i = 0; i < 4; i++) {
        std::cerr << input[i] << ":\n";
        icesp::graph::adj_matrix g(input[i]);
        std::cerr << g.n << "\n";
        g.dump_to_obj(output[i]);
        icesp::graph::adj_matrix gg(output[i]);
        std::cerr << gg.n << "\n";
        std::cerr << "\n";
    }
}

